﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Web.Security;
using vngoodlife.Util;

namespace vngoodlife.Content.ckeditor
{
    public partial class ImageBrowserUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //ResizeMessage.Text = "";
           // Page.ClientScript.RegisterStartupScript(this.GetType(), "FocusImageList", "window.setTimeout(\"document.getElementById('" + ImageList.ClientID + "').focus();\", 1);", true);
        }
        

        protected void Upload(object sender, EventArgs e)
        {
            if (IsImage(UploadedImageFile.FileName))
            {
                string filename = UniqueFilename(UploadedImageFile.FileName);
                string fullpath = ImageFolder + filename;
                System.Drawing.Image image = System.Drawing.Image.FromStream(UploadedImageFile.PostedFile.InputStream);
                float imgWidth = image.PhysicalDimension.Width;

                if (imgWidth > ConstantClass._uploadImageWidth) 
                {
                    imgWidth = ConstantClass._uploadImageWidth;
                }
                UploadedImageFile.SaveAs(FileImageFolder + filename);

                byte[] data = ImageMedia.Create(UploadedImageFile.FileBytes).Resize(Convert.ToInt16(imgWidth), null).ToByteArray();
                FileStream file = File.Create(FileImageFolder + filename);
                file.Write(data, 0, data.Length);
                file.Close();

                Image1.ImageUrl = ImageFolder + filename + "?" + new Random().Next(1000);
                OkButton.OnClientClick = "window.top.opener.CKEDITOR.dialog.getCurrent().setValueOf('info', 'txtUrl', encodeURI('" + ImageFolder + filename +"')); window.top.close(); window.top.opener.focus();";
            }
        }

        protected void Clear(object sender, EventArgs e)
        {
            Session.Remove("viewstate");
        }

        //util methods
        private bool IsImage(string file)
        {
            return file.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase) ||
                file.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase) ||
                file.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase);
        }

        protected string UniqueFilename(string filename)
        {
            string newfilename = filename;

            for (int i = 1; File.Exists(FileImageFolder + newfilename); i++)
            {
                newfilename = filename.Insert(filename.LastIndexOf('.'), "(" + i + ")");
            }

            return newfilename;
        }

        protected string UniqueDirectory(string directoryname)
        {
            string newdirectoryname = directoryname;

            for (int i = 1; Directory.Exists(FileImageFolderRoot + newdirectoryname); i++)
            {
                newdirectoryname = directoryname + "(" + i + ")";
            }

            return newdirectoryname;
        }

        //properties
        protected string ImageFolderRoot
        {
            get { return ResolveUrl(ConfigurationManager.AppSettings["ImageRoot"]); }
        }

        protected string FileImageFolderRoot
        {
            get { return Server.MapPath(ImageFolderRoot); }
        }

        protected string ImageFolder
        {
            get { return ImageFolderRoot + ViewState["folder"]; }
            set { ViewState["folder"] = value; }
        }

        protected string FileImageFolder
        {
            get { return Server.MapPath(ImageFolder); }
        }
    }
}